//
//  HomeViewController.swift
//  MT
//
//  Created by ooops on 2019/7/18.
//  Copyright © 2019年 张强. All rights reserved.
//

import UIKit
import YPNavigationBarTransition
import MTCategory
import JXPagingView
import JXCategoryView
import DeviceKit
import JXSegmentedView


class MTChatMessageViewController: BaseViewController {
    
    var naviDataSource = JXSegmentedTitleDataSource()
    lazy var naviSegmentedView = JXSegmentedView()
    lazy var listContainer = JXSegmentedListContainerView(dataSource: self, type: .scrollView)

    var lists: [JXSegmentedListContainerViewListDelegate] = [JCConversationListViewController(), JCContactsViewController()]
    
    lazy var rightBtn: UIButton = {
        let btn: UIButton = UIButton(type: .custom)
        btn.setImage(UIImage(named: "icon-addfriend"), for: .normal)
        btn.addTarget(self, action: #selector(_clickNavRightButton(_:)), for: .touchUpInside)
        //btn.frame = CGRect(x: 0, y: 0, width: 40, height: 20)
        btn.sizeToFit()
        btn.adjustImage(position: .right, spacing: 1)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let item = UIBarButtonItem(customView: rightBtn)
        self.navigationItem.rightBarButtonItems = [item]
        let titles = ["私聊", "好友"]
        naviDataSource.titles = titles
        naviDataSource.itemSpacing = 0
        naviDataSource.itemContentWidth = 50
        naviDataSource.titleNormalColor = UIColor.text.x9b9b9b
        naviDataSource.titleSelectedColor = UIColor.text.x1d1a1a
        naviDataSource.isTitleZoomEnabled = true
        naviDataSource.titleNormalFont = PingFangSCSemiboldFont(16)
        naviDataSource.titleSelectedFont = PingFangSCSemiboldFont(16)

        naviSegmentedView.contentEdgeInsetLeft = 15
        naviSegmentedView.contentEdgeInsetRight = 15
        naviSegmentedView.frame = CGRect(x: 0, y: 0, width: 140, height: 30)
        naviSegmentedView.dataSource = naviDataSource
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = UIColor.text.x1d1a1a
        indicator.indicatorWidth = 20
        indicator.indicatorHeight = 3
        naviSegmentedView.indicators = [indicator]

        navigationItem.titleView = naviSegmentedView

        view.addSubview(listContainer)

        naviSegmentedView.listContainer = listContainer
        listContainer.scrollView.isScrollEnabled = false
        
//        OpenInstallSDK.defaultManager()?.getInstallParmsCompleted({ (appData) in
//            DispatchQueue.main.async {
//                if appData?.data != nil {
//                    //e.g.如免填邀请码建立邀请关系、自动加好友、自动进入某个群组或房间等
//                }
//                if appData?.channelCode != nil{
//                   //e.g.可自己统计渠道相关数据等
//                }
//                let c = UIApplication.shared.keyWindow!.rootViewController!
//                debugPrint("安装参数 data = \(String(describing: appData?.data)),channelCode = \(String(describing: appData?.channelCode))")
//                UIAlertController.rx.show(in: c, title: "安装参数", message: String(describing: appData?.data), buttons: [.cancel(""), .default("去充值")]).subscribe(onSuccess: { index in
//
//                })
//                .disposed(by: globalDisposedBag)
//            }
//        })
    }
    
    @objc func _clickNavRightButton(_ sender: UIButton) {
//        navigationController?.pushViewController(JCSearchFriendViewController(), animated: true)
        let c: MTSearchAddFriendViewController = MTSearchAddFriendViewController()
        let n: BaseNavController = BaseNavController(rootViewController: c)
        n.modalPresentationStyle = .fullScreen
        n.modalTransitionStyle = .crossDissolve
        navigationController?.present(n, animated: true, completion: nil)
//        navigationController?.pushViewController(MTSearchAddFriendViewController(), animated: true)
//        let c = MTPaymentViewController()
//        navigationController?.pushViewController(c, animated: true)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        listContainer.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}

extension MTChatMessageViewController: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return 2
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        return lists[index]
    }
}

extension MTChatMessageViewController {
    override func yp_navigtionBarConfiguration() -> YPNavigationBarConfigurations {
        return [.backgroundStyleColor, .backgroundStyleOpaque, .showShadowImage]
    }
}
